﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Presentation.Web.Admin.Areas.Open.Controllers
{
    public class PicController : Infrastructure.Crosscutting.NetFramework.Controllers.OpenBaseController
    {
        Application.Admin.Pic.IPicInfoService picService;

        Application.Admin.Pic.IOpenPicService openPicService;

        public PicController(Application.Admin.Pic.IPicInfoService pic,
            Application.Admin.Pic.IOpenPicService openPic)
        {
            if (pic == null)
            {
                throw new ArgumentNullException("picService is null");
            }
            if (openPic == null)
            {
                throw new ArgumentNullException("openPicService is null");
            }
            picService = pic;
            openPicService = openPic;
        }

        public JsonResult GetOpenPicById(int id)
        {
            var data = openPicService.GetOpenPicById(id);
            if (data == null)
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetOpenPicByName(string name)
        {
            var temp = picService.GetList().Where(e => e.Name == name).FirstOrDefault();
            if (temp != null)
            {
                var data = openPicService.GetOpenPicById(temp.Id);
                if (data == null)
                {
                    return Json("", JsonRequestBehavior.AllowGet);
                }
                return Json(data, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetOpenPicThumbByPicId(int id, int width, int height)
        {
            var data = openPicService.GetOpenPicThumbByPicId(id, width, height);
            if (data == null)
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(data, JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetOpenPicThumbListByPicId(int id)
        {
            var data = openPicService.GetOpenPicThumbListByPicId(id);
            if (data == null)
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(data, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
